<template>
<div id="cont">
  <div class="s-main">
  	<form enctype="multipart/form-data" method="post">
  	  <input type="file" multiple name="files" id="files" ref="files" @change="upload">
  	</form>
  </div>
</div>
</template>

<script>
import app from '@/utils/index';
import { uploadImage, uploadVideo } from '@/api/system_api'

export default {
	name: 'upload',
	props: ['type'],
	data() {
		return {
			msg: ''
		}
	},
	methods: {
		upload(e) {
			const files = e.target.files || e.dataTransfer.files,
						mt = files[0].type.split('/')[0];
			let iUrl = '';
			// console.log('25............', files[0]);
			if(!files.length) {
				return;
			}
			if(mt != this.type) {
				return app.flash('类型错误~');
			}
			const formdata = new FormData();
			formdata.append(mt, files[0]);
			if(mt == 'image') {
				uploadImage(formdata).then(response => {
					const res = response.data;
					if(res.s) {
						const params = {
							type: mt,
							url: res.d
						}
						this.$emit('preview', params);
					}
				})
			}else if(mt == 'video') {
				uploadVideo(formdata).then(response => {
					const res = response.data;
					if(res.s) {
						const params = {
							type: mt,
							url: res.d
						}
						this.$emit('preview', params);
					}
				})
			}
			
		},
		removeFiles() {
			this.$refs.files.value = '';
		}
  }
}
</script>